package cn.group.service.impl;

import cn.group.dao.ClassDao;
import cn.group.dao.StudentDao;
import cn.group.domain.Classes;
import cn.group.domain.Student;
import cn.group.domain.Teacher;
import cn.group.service.StudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StudentServiceImpl implements StudentService {

    private StudentDao studentDao;
    private ClassDao classDao;

    @Autowired
    public void setStudentDao(StudentDao studentDao,ClassDao classDao) {
        this.studentDao = studentDao;
        this.classDao = classDao;
    }

    /*获得所有学生，并实现分页*/
    @Override
    public PageInfo<Student> queryAllStu(Integer currentPage,Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<Student> students = studentDao.queryAllStu();
        //将查询出来的对象交给分页插件
        PageInfo<Student> pageInfo = new PageInfo<>(students);
        System.out.println("查询到的学生信息：");
        for (Student student : pageInfo.getList()) {
            System.out.println(student);
        }
        return pageInfo;
    }

    /*事务管理根据id获取一个学生信息，并获取所有班级信息的操作*/
    @Transactional
    @Override
    public Map<String,Object> queryOneStuById(Integer id) {
        Map<String,Object> map = new HashMap<>();
        //根据id获取一个学生信息
        Student student = studentDao.queryOneStuByid(id);
        if(student != null){
            map.put("code",0);
            map.put("student",student);
        }else {
            map.put("code",-1);
        }
        //获取所有班级信息
        List<Classes> classes = classDao.queryAll();
        map.put("classes",classes);
        //System.out.println(classes);
        return map;
    }

    /*更新一个学生信息*/
    @Override
    public Boolean updateStu(Student stu) {
        System.out.println(stu);
        Integer i = studentDao.UpdateStu(stu);
        return i>0;
    }

    /*删除一个学生*/
    @Override
    public Boolean deleteStu(Integer id) {
        Integer i = studentDao.deleteStu(id);
        return i > 0;
    }

    /*增加一个学生*/
    @Override
    public boolean addUser(Student stu) {
        Integer i = studentDao.addUser(stu);
        return i > 0;
    }

    /*根据输入的条件查询学生信息*/
    @Override
    public PageInfo<Student> queryStuByCondition(Integer currentPage, Integer pageSize, String keyword) {
        PageHelper.startPage(currentPage,pageSize);
        List<Student> students = studentDao.queryStuByCondition(keyword);
        PageInfo<Student> pageInfo = new PageInfo<>(students);
        System.out.println("查询到的学生信息：");
        for (Student student : pageInfo.getList()) {
            System.out.println("获取的学生："+student);
        }
        return pageInfo;
    }

    @Override
    public Student queryByUsername(String username) {
        Student student = studentDao.queryStuByUsername(username);
        return student;
    }

    @Override
    public PageInfo<Student> queryStuByTId(Integer currentPage, Integer pageSize, Integer uid) {
        PageHelper.startPage(currentPage,pageSize);
        List<Student> students = studentDao.queryStuByTId(uid);
        //将查询出来的对象交给分页插件
        PageInfo<Student> pageInfo = new PageInfo<>(students);
        System.out.println("查询到的学生信息：");
        for (Student student : pageInfo.getList()) {
            System.out.println(student);
        }
        return pageInfo;
    }
}
